class ReturnToTheDictionary {
    constructor() {
        this.dictionary = {
            success: {code: 200, message: 'success'},
            theCityQueryFailed: {code: 201, message: '城市查询失败，请尝试手动查询城市名'},
            parametersAreMissing: {code: 202, message: '缺少必要的参数'},
            citiesDoNotExist: {code: 203, message: '城市不存在'},
            parentCityNotExist: {code: 204, message: '父级城市不存在'},
            reIsNoSuchCityLevel: {code: 205, message: '没有此城市等级'},
            weatherNotExist: {code: 206, message: '天气不存在'},
        };

        // 动态添加方法
        for (const key in this.dictionary) {
            if (Object.prototype.hasOwnProperty.call(this.dictionary, key)) {
                ReturnToTheDictionary.prototype[key] = async (ctx) => {
                    const value = this.getValue(key);
                    return this.dictionaryProcessing(ctx, value);
                };
            }
        }
    }

    getValue(key) {
        //拷贝一份，不然会传递源对象，后面修改会影响源对象
        return JSON.parse(JSON.stringify(this.dictionary[key]));
    }

    async dictionaryProcessing(ctx, value) {
        const parameter = {status: 200, body: value};
        // 合并参数
        return Object.assign(ctx, parameter);
    }
}

module.exports = new ReturnToTheDictionary();
